package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.SpringitemDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class SpringitemView {
    private InputText txtCodigoItem_Item;
    private InputText txtCodigoSprint_Sprint;
    private InputText txtCodigoSprint;
    private InputText txtCodigoItem;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<SpringitemDTO> data;
    private SpringitemDTO selectedSpringitem;

    public SpringitemView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            SpringitemDTO springitemDTO = (SpringitemDTO) e.getObject();

            if (txtCodigoItem_Item == null) {
                txtCodigoItem_Item = new InputText();
            }

            txtCodigoItem_Item.setValue(springitemDTO.getCodigoItem_Item());

            if (txtCodigoSprint_Sprint == null) {
                txtCodigoSprint_Sprint = new InputText();
            }

            txtCodigoSprint_Sprint.setValue(springitemDTO.getCodigoSprint_Sprint());

            if (txtCodigoSprint == null) {
                txtCodigoSprint = new InputText();
            }

            txtCodigoSprint.setValue(springitemDTO.getCodigoSprint());

            if (txtCodigoItem == null) {
                txtCodigoItem = new InputText();
            }

            txtCodigoItem.setValue(springitemDTO.getCodigoItem());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtCodigoItem_Item != null) {
            txtCodigoItem_Item.setValue(null);
            txtCodigoItem_Item.setDisabled(true);
        }

        if (txtCodigoSprint_Sprint != null) {
            txtCodigoSprint_Sprint.setValue(null);
            txtCodigoSprint_Sprint.setDisabled(true);
        }

        if (txtCodigoSprint != null) {
            txtCodigoSprint.setValue(null);
            txtCodigoSprint.setDisabled(false);
        }

        if (txtCodigoItem != null) {
            txtCodigoItem.setValue(null);
            txtCodigoItem.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Springitem entity = null;

        try {
            SpringitemId id = new SpringitemId();
            id.setCodigoSprint((((txtCodigoSprint.getValue()) == null) ||
                (txtCodigoSprint.getValue()).equals("")) ? null
                                                         : new String(
                    txtCodigoSprint.getValue().toString()));
            id.setCodigoItem((((txtCodigoItem.getValue()) == null) ||
                (txtCodigoItem.getValue()).equals("")) ? null
                                                       : new String(
                    txtCodigoItem.getValue().toString()));
            entity = BusinessDelegatorView.getSpringitem(id);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtCodigoItem_Item.setDisabled(false);
            txtCodigoSprint_Sprint.setDisabled(false);
            txtCodigoSprint.setDisabled(false);
            txtCodigoItem.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtCodigoItem_Item.setValue(entity.getItem().getCodigoItem());
            txtCodigoItem_Item.setDisabled(false);
            txtCodigoSprint_Sprint.setValue(entity.getSprint().getCodigoSprint());
            txtCodigoSprint_Sprint.setDisabled(false);
            txtCodigoSprint.setValue(entity.getId().getCodigoSprint());
            txtCodigoSprint.setDisabled(true);
            txtCodigoItem.setValue(entity.getId().getCodigoItem());
            txtCodigoItem.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveSpringitem(FacesUtils.checkString(
                    txtCodigoSprint), FacesUtils.checkString(txtCodigoItem),
                FacesUtils.checkString(txtCodigoItem_Item),
                FacesUtils.checkString(txtCodigoSprint_Sprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteSpringitem(FacesUtils.checkString(
                    txtCodigoSprint), FacesUtils.checkString(txtCodigoItem));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateSpringitem(FacesUtils.checkString(
                    txtCodigoSprint), FacesUtils.checkString(txtCodigoItem),
                FacesUtils.checkString(txtCodigoItem_Item),
                FacesUtils.checkString(txtCodigoSprint_Sprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoSprint == null) {
                txtCodigoSprint = new InputText();
            }

            txtCodigoSprint.setValue(selectedSpringitem.getCodigoSprint());

            if (txtCodigoItem == null) {
                txtCodigoItem = new InputText();
            }

            txtCodigoItem.setValue(selectedSpringitem.getCodigoItem());

            BusinessDelegatorView.deleteSpringitem(FacesUtils.checkString(
                    txtCodigoSprint), FacesUtils.checkString(txtCodigoItem));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedSpringitem);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoSprint, String codigoItem,
        String codigoItem_Item, String codigoSprint_Sprint)
        throws Exception {
        try {
            BusinessDelegatorView.updateSpringitem(codigoSprint, codigoItem,
                codigoItem_Item, codigoSprint_Sprint);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("SpringitemView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtCodigoItem_Item() {
        return txtCodigoItem_Item;
    }

    public void setTxtCodigoItem_Item(InputText txtCodigoItem_Item) {
        this.txtCodigoItem_Item = txtCodigoItem_Item;
    }

    public InputText getTxtCodigoSprint_Sprint() {
        return txtCodigoSprint_Sprint;
    }

    public void setTxtCodigoSprint_Sprint(InputText txtCodigoSprint_Sprint) {
        this.txtCodigoSprint_Sprint = txtCodigoSprint_Sprint;
    }

    public InputText getTxtCodigoSprint() {
        return txtCodigoSprint;
    }

    public void setTxtCodigoSprint(InputText txtCodigoSprint) {
        this.txtCodigoSprint = txtCodigoSprint;
    }

    public InputText getTxtCodigoItem() {
        return txtCodigoItem;
    }

    public void setTxtCodigoItem(InputText txtCodigoItem) {
        this.txtCodigoItem = txtCodigoItem;
    }

    public List<SpringitemDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataSpringitem();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<SpringitemDTO> springitemDTO) {
        this.data = springitemDTO;
    }

    public SpringitemDTO getSelectedSpringitem() {
        return selectedSpringitem;
    }

    public void setSelectedSpringitem(SpringitemDTO springitem) {
        this.selectedSpringitem = springitem;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
